//
//  ViewModel.m
//  Rac
//
//  Created by ios on 2017/3/22.
//  Copyright © 2017年 Wy. All rights reserved.
//

#import "ViewModel.h"
#import "HeadView.h"
#import "TableView.h"

@implementation ViewModel


-(instancetype)init{
    self = [super init];
    if (self) {
        _data = [NSMutableArray array];
        
        [self initViewModel];
        
    }
    return self;
}

-(void)initViewModel{
    
    @weakify(self);
    
    self.refreshCommand  =  [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        @strongify(self)
        //此处  网络请求获取数据源  上下拉刷新更新数据源
        self.page = [input[1] integerValue];
        
        if (self.page == 1) {
            [self.data removeAllObjects];
        }
        
        for (NSInteger i = 0; i< 5; i++) {
            
            NSDictionary *dic = @{@"name":[NSString stringWithFormat:@"第%ld页怪兽",self.page],@"age":@(i+20)};
            Model  *mod = [[Model alloc] initWithDictionary:dic];
            
            [self.data addObject:mod];
        }
        
        TableView *tableView = input[0];
        [tableView reloadData];
        
        [tableView.mj_header endRefreshing];
        [tableView.mj_footer endRefreshing];
        
        return [RACSignal empty];
    }];
    
    self.itemClickCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        @strongify(self)
        
        HeadView * view  = input[1];
        
        NSInteger index  =  [input[0] integerValue];
        Model *mod = self.data[index];
        
        view.bgLabel.text = [NSString stringWithFormat:@"--%@:  %ld岁--",mod.name,mod.age];
        
        return [RACSignal empty];
    }];
    
    
    
}


@end
